﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace AutoCadDraw
{
    public static class ExtendedFunction
    {
        public static Polyline ToAutoCadEntity(this lclBridgeTool.Polyline polyline)
        {
            Polyline aupolyline = new Polyline();
            for (int i = 0; i < polyline.NumberOfVertices; i++)
            {
                lclBridgeTool.Point2d point2d = polyline.GetPoint2dAt(i);
                Point2d point2D = new Point2d(point2d.X, point2d.Y);
                double v = polyline.GetBulgeAt(i);
                double sw = polyline.GetStartWidthAt(i);
                double ew = polyline.GetEndWidthAt(i);
                aupolyline.AddVertexAt(aupolyline.NumberOfVertices, point2D, v, sw, ew);
            }
            aupolyline.Closed = polyline.Closed;
            return aupolyline;
        }
        public static Line ToAutoCadEntity(this lclBridgeTool.Line line)
        {
            Point3d startPoint = new Point3d(line.StartPoint.X, line.StartPoint.Y, 0);
            Point3d endPoint = new Point3d(line.EndPoint.X, line.EndPoint.Y, 0);
            Line line1 = new Line(startPoint, endPoint);
            return line1;
        }
        public static Entity ToAutoCadEntity(this lclBridgeTool.Entity entity)
        {
            Entity entity1 = null;
            if (entity is lclBridgeTool.Polyline polyline)
            {
                entity1 = ToAutoCadEntity(polyline);
            }
            else if (entity is lclBridgeTool.Line line)
            {
                entity1 = ToAutoCadEntity(line);
            }
            else
            {
                
            }
            return entity1;
        }

        public static List<lclBridgeTool.Entity> ToListEntity(this string json)
        {
            List<lclBridgeTool.Entity> entities = new List<lclBridgeTool.Entity>();
            List<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();//当前应用程中所有程序集
            List<Assembly> aslclFoundation = assemblies.Where(x => x.ManifestModule.Name.Equals("lclFoundation.dll")).ToList();//找到lclFoundation.dll
            List<Assembly> asDrawPaperKit = assemblies.Where(x => x.ManifestModule.Name.Equals("DrawPaperKitBridge.dll")).ToList();//DrawPaperKitBridge.dll
            List<Assembly> assemblies1 = new List<Assembly>();
            assemblies1.AddRange(aslclFoundation);
            assemblies1.AddRange(asDrawPaperKit);
            JArray jArray = JsonConvert.DeserializeObject<JArray>(json);
            for (int i = 0; i < jArray.Count; i++)
            {
                string entityJson = JsonConvert.SerializeObject(jArray[i]);

                lclBridgeTool.Entity entityObj = (lclBridgeTool.Entity)Tool.JsonToExample(assemblies1, "lclBridgeTool", entityJson);
                entities.Add(entityObj);
            }
            return entities;
        }
    }
}
